package ControlLayer;

import java.util.ArrayList;

import DBLayer.DBConnection;
import DBLayer.DBSupplier;
import DBLayer.IFDBSupplier;
import ModelLayer.Supplier;

public class SupplierCtr {
	public Supplier getSupplier(int supplierId) {
		IFDBSupplier dbSupplier = new DBSupplier();
		return dbSupplier.getSupplier(supplierId, true);
	}
	public void insertSupplier(String name, String address, String country, long phone, String email) {
		Supplier supplierObj = new Supplier();
		supplierObj.setName(name);
		supplierObj.setAddress(address);
		supplierObj.setCountry(country);
		supplierObj.setPhone(phone);
		supplierObj.setEmail(email);
		try {
			DBConnection.startTransaction();
			DBSupplier dbSupplier = new DBSupplier();
			dbSupplier.insertSupplier(supplierObj);
			DBConnection.commitTransaction();
		} catch (Exception e) {
			DBConnection.rollbackTransaction();
		}
	}
	public ArrayList<Supplier> getAllSuppliers() {
		IFDBSupplier dbSupplier = new DBSupplier();
		ArrayList<Supplier> suppliers = new ArrayList<Supplier>();
		suppliers = dbSupplier.getAllSuppliers(false);
		return suppliers;
	}
	public int updateSupplier(int supplierId, String name, String address, 
			String country, long phone, String email) {
		IFDBSupplier dbSupplier = new DBSupplier();
		Supplier supplierObj = new Supplier();
		supplierObj.setId(supplierId);
		supplierObj.setName(name);
		supplierObj.setAddress(address);
		supplierObj.setCountry(country);
		supplierObj.setPhone(phone);
		supplierObj.setEmail(email);
		return dbSupplier.updateSupplier(supplierObj);
		}
	
	public void deleteSupplier(int supplierId) {
		IFDBSupplier dbSupplier = new DBSupplier();
		dbSupplier.deleteSupplier(supplierId);
	}
}
